package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee e = service.getOne(wrapper);
        if (e == null) {
            return R.error("登录失败！！");
        }
        if (!e.getPassword().equals(password)) {
            return R.error("登录失败！！");
        }
        if (e.getStatus() == 0) {
            return R.error("账户已禁用！！");
        }
        request.getSession().setAttribute("employee", e.getId());
        return R.success(e);
    }

    //退出
    @PostMapping("/logout")
    private R<String> logout(HttpServletRequest request) {
//        request.getSession().invalidate();
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！！！");
    }

    //添加员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {

        log.info("新增员工,员工信息：{}", employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        service.save(employee);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Employee::getName, name);
        if (name == null) {
            service.page(pageInfo);
            return R.success(pageInfo);
        }
        service.page(pageInfo, wrapper);
//       Page<Employee> pageInfo = service.page(new Page<Employee>(page, pageSize), new LambdaQueryWrapper<Employee>().like(Employee::getName, name));
        return R.success(pageInfo);
    }

    //启用/禁用员工
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        Long id = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        service.updateById(employee);
        return R.success("修改成功！！");
    }


    @GetMapping("/{id}")
    public R<Employee> getById( @PathVariable Long id)
    {
        log.info("根据id查询员工信息...");
        Employee employee = service.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
